/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.view;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;

import com.yc.video.ResourceTable;
import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.tool.BaseToast;
import com.yc.video.tool.PlayerUtils;

/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/11/9
 *     desc  : 自动播放完成界面
 *     revise:
 * </pre>
 */
public class CustomCompleteView extends StackLayout implements InterControlView, Component.ClickedListener {

    private static final int UNSPECIFIED = -1;
    private static final int LANDSCAPE = 0;
    private static final int PORTRAIT = 1;
    private static final int FOLLOWRECENT = 3;
    private static final int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8; // 没找到对应的值，暂时模拟成

    private Context mContext;
    private ControlWrapper mControlWrapper;
    private Image mIvStopFullscreen;
    private DirectionalLayout mLlReplay;
    private DirectionalLayout mLlShare;

    public CustomCompleteView(Context context) {
        super(context);
        init(context);
    }

    public CustomCompleteView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    public CustomCompleteView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setVisibility(HIDE);
        Component component = LayoutScatter.getInstance(mContext).parse(
            ResourceTable.Layout_custom_video_player_completed, this, true);
        initFindViewById(component);
        initListener();
        setClickable(true);
    }

    private void initFindViewById(Component view) {
        mIvStopFullscreen = (Image) view.findComponentById(ResourceTable.Id_iv_stop_fullscreen);
        mLlReplay = (DirectionalLayout) view.findComponentById(ResourceTable.Id_ll_replay);
        mLlShare = (DirectionalLayout) view.findComponentById(ResourceTable.Id_ll_share);
    }

    private void initListener() {
        setContentEnable(true);
        mLlReplay.setClickedListener(this);
        mLlShare.setClickedListener(this);
        mIvStopFullscreen.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (component == mLlReplay) {
            // 点击重播
            mControlWrapper.replay(true);
        } else if (component == mLlShare) {
            // 点击分享
            BaseToast.showRoundRectToast("点击分享，后期完善");
        } else if (component == mIvStopFullscreen) {
            // 点击返回键
            if (mControlWrapper.isFullScreen()) {
                Ability activity = PlayerUtils.scanForActivity(mContext);
                if (activity != null) {
                    activity.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
                    mControlWrapper.stopFullScreen();
                }
            }
        }
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayStateChanged(int playState) {
        if (playState == ConstantKeys.CurrentState.STATE_BUFFERING_PLAYING) {
            setVisibility(VISIBLE);
            mIvStopFullscreen.setVisibility(mControlWrapper.isFullScreen() ? VISIBLE : HIDE);
            moveChildToFront(this);
        } else {
            setVisibility(HIDE);
        }
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        if (playerState == ConstantKeys.PlayMode.MODE_FULL_SCREEN) {
            mIvStopFullscreen.setVisibility(VISIBLE);
        } else if (playerState == ConstantKeys.PlayMode.MODE_NORMAL) {
            mIvStopFullscreen.setVisibility(HIDE);
        }

        Ability activity = PlayerUtils.scanForActivity(mContext);
        if (activity != null && mControlWrapper.hasCutout()) {
            int orientation = activity.getDisplayOrientation();
            int cutoutHeight = mControlWrapper.getCutoutHeight();
            DirectionalLayout.LayoutConfig sflp = (DirectionalLayout.LayoutConfig) mIvStopFullscreen.getLayoutConfig();
            if (orientation == PORTRAIT) {
                sflp.setMargins(0, 0, 0, 0);
            } else if (orientation == LANDSCAPE) {
                sflp.setMargins(cutoutHeight, 0, 0, 0);
            } else if (orientation == SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                sflp.setMargins(0, 0, 0, 0);
            }
        }
    }

    @Override
    public void setProgress(int duration, int position) {
    }

    @Override
    public void onLockStateChanged(boolean isLock) {
    }
}
